home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 020a / gifhdr.zip / GIFHDR.C next >
Text File  |  1989-07-18  |  3KB  |  128 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5.  
  6.  
  7. #define byte char
  8. #define SIG_SIZ 6
  9. #define GIF_SIG "GIF87a"
  10.  
  11.  
  12.  
  13. union clr_info {
  14.   unsigned m:1;             /* m = 1, golbal color map follows descriptor */
  15.   unsigned cr:3;            /* cr+1 = # bits of color resolution */
  16.   unsigned ub:1;            /* unused bit, should be 0 */
  17.   unsigned pixel:3;         /* pixel+1 = # bits/pixel in image */
  18.   byte clr_byte;
  19. };
  20.  
  21.  
  22.  
  23. struct scr_des {
  24.   int width;                  /* Raster width in pixels */
  25.   int height;                 /* Raster height in pixels */
  26.   union clr_info color_info;  /* Color information */
  27.   byte background;            /* background=Color index of screen background
  28.                  (color is defined from the global color map
  29.                  or default map if none specified */
  30.   byte des_end;               /* End of descriptor 0 0 0 0 0 0 0 */
  31. };
  32.  
  33.  
  34.  
  35. struct map {
  36.   byte red;
  37.   byte green;
  38.   byte blue;
  39. };
  40.  
  41.  
  42.  
  43. FILE *gif_file;
  44.  
  45.  
  46.  
  47. main()
  48. {
  49.   struct scr_des screen_descriptor;
  50.   struct map *color_map;
  51.   int clr_map_entries;
  52.  
  53.   if (read_sig ("TEST.GIF")) {
  54.     if (read_screen_descriptor (&screen_descriptor)) {
  55.       cprintf ("screen width:           %d\r\n", screen_descriptor.width);
  56.       cprintf ("screen height:          %d\r\n", screen_descriptor.height);
  57.       cprintf ("gobal color map?:       %u\r\n", screen_descriptor.color_info.m);
  58.       cprintf ("bits of color res:      %u\r\n", screen_descriptor.color_info.cr+1);
  59.       cprintf ("# bits/pixel in image:  %u\r\n", screen_descriptor.color_info.pixel+1);
  60.       cprintf ("clr index of scr bg:    %d\r\n", screen_descriptor.background);
  61.       if (screen_descriptor.color_info.m) {
  62.     cprintf ("Yes there is a color map.\r\n");
  63.     clr_map_entries = 2^(screen_descriptor.color_info.pixel+1);
  64.     color_map = (struct map *) malloc (clr_map_entries);
  65.     read_color_map (color_map, clr_map_entries);
  66.       }
  67.     }
  68.   }
  69.   getch();
  70. }
  71.  
  72.  
  73.  
  74.  
  75. int read_sig (char *file_name)
  76. {
  77.   char signature [SIG_SIZ+1];
  78.  
  79.   gif_file = fopen (file_name, "rb");
  80.   if (!gif_file) {
  81.     cprintf ("File not found.\r\n");
  82.     fclose (gif_file);
  83.     return (0);
  84.   }
  85.   else {
  86.     fread (signature, SIG_SIZ, 1, gif_file);
  87.     signature [SIG_SIZ] = '\0';
  88.     if (strcmp (signature, GIF_SIG)) {
  89.       cprintf ("This is not a valid GIF image file\r\n");
  90.       fclose (gif_file);
  91.       return (0);
  92.     }
  93.     else return (1);
  94.   }
  95. }
  96.  
  97.  
  98.  
  99. int read_screen_descriptor (struct scr_des *descriptor)
  100. {
  101.   int result;
  102.  
  103.   result = fread (descriptor, sizeof (struct scr_des), 1, gif_file);
  104.   if (result == 1)
  105.     return (1);
  106.   else {
  107.     cprintf ("Error in reading screen descriptor.\r\n");
  108.     fclose (gif_file);
  109.     return (0);
  110.   }
  111. }
  112.  
  113.  
  114.  
  115. int read_color_map (struct map *clr_map, int num)
  116. {
  117.   int result;
  118.  
  119.   result = fread (clr_map, sizeof (struct map), num, gif_file);
  120.   if (result == num)
  121.     return (1);
  122.   else {
  123.     cprintf ("Error in reading color map.\r\n");
  124.     fclose (gif_file);
  125.     return (0);
  126.   }
  127. }
  128.